<template>
  <div>
    <Loading v-if="menus.length == 0" />
    <div id="top" :class="{ fixed: isfixed }">
      <!-- <router-link tag="p" :to="{name:'home'}">首页</router-link> -->

      <p @click="goback">首页</p>
      <van-search
        class="resech"
        v-model="value"
        shape="round"
        placeholder="搜索菜品"
      />
    </div>
    <div class="content">
      <ul class=" left">
        <li
          v-for="(item, index) in until"
        >
          {{ item }}
        </li>
      </ul>
      <!-- 绿色固定高度 -->
      <div
        class="wrapper"
        ref="wrapper"
        :style="{ overflow: 'auto', height: height + 'px' }"
      >
        <!-- 内容容器，随着项变多，而自动变高 -->
        <div class="container">
          <template v-for="(item, index) in menus">
            <!-- item项 -->
            <ul class="right">
              <p>
                <img v-lazy="item.ImageUrl" v-image-preview :src="item.ImageUrl" />
              </p>
              <li>
                <div>{{ item.name | mysubstr(9) }}</div>
                <div>
                  <span>{{ item.ingredient | mysubstr(5) }}</span>
                </div>
                <div>
                  <span>{{ item.Desc | mysubstr(5) }}</span>
                  <i @click="addFn(index)">+</i>
                  <b>{{ item.count }}</b>
                 
                  <i @click="descFn(index)">-</i>
                </div>
                <div>
                  <span>￥{{ item.currentPrice }}</span>
                  <del>￥{{ item.originPrice }}</del>
                </div>
                <div>
                  <span>{{ item.PromotionInfo }}</span>
                </div>
              </li>
              <i></i>
            </ul>
          </template>
        </div>
      </div>
    </div>
    <!-- 购物车 -->
    <div class="money">
      <p class="next" @click="shoppingCar">购物车</p>
      <!-- <router-link tag="p" class="now" :to="{name:'home'}">去下单</router-link> -->
      <p class="now" @click="shoppingCar">去下单</p>
      <i class="iconfont">&#xe601;</i>
      <p class="num">{{ numTotal }}</p>
    </div>

  </div>
</template>
<script>
import Vue from "vue";
import { Search } from "vant";
Vue.use(Search);
import { ActionSheet } from "vant";
Vue.use(ActionSheet);
import BScroll from "better-scroll";

export default {
  data() {
    return {
      // click: false,
      show: false,
      value: "",
      height: 500,
      bs: null,
      pageNum: 1,
      num: 0,
      numm: 1,
      isfixed: false,
      menus: [],
      number: 0,
      until: ["热销榜", "汉堡包", "鸡肉卷", "饮品"],
      numTotal:0
    };
  },
  created() {
    // 进入到预约页面中后，让底部菜单隐藏
    // this.$eventBus.$emit("hideMenu", false);
    this.$store.state.footerNavShow = false
  },
  async mounted() {
    let h = document.documentElement.clientHeight - 120;
    this.height = h;
    // 吸顶
    window.addEventListener("scroll", (e) => {
      let scrollTop = document.documentElement.scrollTop;
      if (scrollTop > 100) {
        this.isfixed = true;
      } else {
        this.isfixed = false;
      }
    });
    // 获取数据
    let rew = await this.$api.getMenuList();
    this.menus = rew.data;
    // 滚动
    this.$nextTick(() => {
      this.bs = new BScroll(this.$refs["wrapper"], {
        // 默认better-scroll会让click事件不生效，需要开启
        click: true,
        // 解决移动站点中300ms的延时点击问题
        // tap: "tap",
        // 滚动事件触发时机
        probeType: 1,
        // 拖动到底部配置
        pullUpLoad: true,
        // 下拉配置选项
        pullDownRefresh: true,
      });
    });
  },
  methods: {
    tab(index) {
      this.num = index;
    },
    descFn(index){
      this.menus[index].count<=0? this.menus[index].count=0: this.menus[index].count--

      this. numTotal=0

      for(let i = 0 ; i<this.menus.length;i++){
        this. numTotal += this.menus[i].count
      }
    },
    addFn(index){
      this.menus[index].count++
      this. numTotal=0
      for(let i = 0 ; i<this.menus.length;i++){
        this. numTotal += this.menus[i].count
      }
    },
    shoppingCar(){
      let newOrder=[]
      for(let i = 0 ; i<this.menus.length;i++){
        if(this.menus[i].count>0){
          newOrder.push(this.menus[i])
        }
      }
      this.$store.state.orderdata=newOrder
      this.$router.push("/confirmOrder")
    },
    goback(){
      this.$store.state.footerNavShow = true
      this.$router.push("/home")
    }

  },
  components: {},
};
</script>
<style lang="scss" scoped>
  * {
  margin: 0;
  padding: 0;
}
html,
body {
  touch-action: none;
  height: 100%;
  ul,
  li {
    list-style: none;
  }
}
.fixed {
  position: fixed;
  top: 0;
  z-index: 2;
}
#top {
  height: 60px;
  border-bottom: 1px solid #f5f5f5;
  background: white;

  p {
    float: left;
    text-align: center;
    width: 100px;
    height: 60px;
    line-height: 60px;
    color: green;
    font-weight: bold;
  }

  .resech {
    margin-top:10px;
    width: 275px;
    float: right;
  }
}

.content {
  .left {
    width: 100px;
    height: 100%;
    background-color: #f6f6f6;
    float: left;

    li {
      width: 100px;
      height: 40px;
      text-align: center;
      line-height: 40px;

      &:hover {
        background-color: white;
      }
    }
  }
  .container {
    overflow: hidden;
    // margin-bottom:5px ;
  }
  .right {
    margin-top: 5px;
    margin-left: 5px;
    float: right;
    height: 100%;
    overflow: hidden;
    // 
    p {
      float: left;
      width: 110px;
      height: 100px;

      img {
        width: 100%;
        height: 100%;
        border-radius: 5px;
      }
    }

    li {
      margin-left: 5px;
      width: 155px;
      height: 100%;
      float: right;

      div:nth-of-type(1) {
        font-size: 14px;
        // font-weight: bold;
      }

      div:nth-of-type(2) {
        font-size: 15px;
        color: #aaa;
      }

      div:nth-of-type(3) {
        font-size: 14px;
        position: relative;
        i:nth-of-type(1) {
          display: inline-block;
          height: 14px;
          width: 14px;
          border-radius: 50%;
          background: #aaa;
          color: white;
          text-align: center;
          line-height: 15px;
          position: absolute;
          top: 5px;
          right: 30px;
        }
        i:nth-of-type(2) {
          display: inline-block;
          height: 14px;
          width: 14px;
          border-radius: 50%;
          background: #aaa;
          color: white;
          text-align: center;
          line-height: 15px;
          position: absolute;
          top: 5px;
          right: 60px;
        }
        b {
          position: absolute;
          top: 5px;
          right: 47.5px;
          font-size: 12px;
        }
      }
      div:nth-of-type(4) {
        span {
          color: red;
          font-size: 20px;
          font-weight: bold;
        }

        del {
          font-size: 12px;
          color: #aaa;
          line-height: 20px;
        }
      }

      div:nth-of-type(5) {
        color: #b2282e;
        font-size: 14px;
      }
    }
  }
}

.money {
  position: fixed;
  bottom: 20px;
  left: 40px;
  width: 300px;
  height: 42px;

  .next {
    background: #666;
    width: 200px;
    border-radius: 20px 0 0 20px;
  }

  .now {
    width: 100px;
    height: 42px;
    background: #ed0d0d;
    border-radius: 0 20px 20px 0;
  }

  p {
    color: #fff;
    float: left;
    height: 100%;
    line-height: 42px;
    text-align: center;
  }
  i {
    color: #ed0d0d;
    font-size: 30px;
    position: absolute;
    left: 22px;
    bottom: 5px;
    /* z-index: 2; */
  }
  .num {
    position: absolute;
    left: 15px;
    bottom: 28px;
    width: 18px;
    height: 18px;
    background: white;
    border-radius: 50%;
    color: #aaa;
    line-height: 20px;
    font-size: 13px;
  }
}
.last {
  height: 150px;
  /* height: 100%; */
}
.hider {
  width: 70px;
  height: 42px;
  position: fixed;
  bottom: 20px;
  left: 190px;
  background: #ed0d0d;
}
.content1 {
  width: 50px;
  height: 50px;
  padding: 0 15px;
  float: left;
  img {
    height: 100%;
    height: 100%;
  }
}
.content2 {
  float: left;
  margin-left: 15px;
  div:nth-of-type(1) {
    font-size: 15px;
    font-weight: bold;
  }
  div:nth-of-type(2) {
    font-size: 14px;
    span {
      color: #ed0d0d;
    }
    del {
      color: #aaa;
      font-size: 12px;
    }
  }
  .bottom {
    position: relative;
    i:nth-of-type(1) {
      display: inline-block;
      height: 16px;
      width: 16px;
      border-radius: 50%;
      background: #aaa;
      color: white;
      text-align: center;
      line-height: 15px;
      position: absolute;
      top: 0px;
      right: 30px;
    }
    i:nth-of-type(2) {
      display: inline-block;
      height: 16px;
      width: 16px;
      border-radius: 50%;
      background: #aaa;
      color: white;
      text-align: center;
      line-height: 15px;
      position: absolute;
      top: 0px;
      right: 70px;
    }
    b {
      position: absolute;
      top: 0px;
      right: 55px;
      font-size: 12px;
    }
  }
}
</style>